package com.cskaoyan.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;

/**
 * 创建日期: 2022/07/06 09:54
 *
 * @author ciggar
 */
public class MybatisMain {

    public static void main(String[] args) throws IOException {

        // 1. 读取核心配置文件
        InputStream inputStream = MybatisMain.class.getClassLoader().getResourceAsStream("mybatis-config.xml");

        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");

        // 2. 获取SqlSessionFactory对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(in);


        // 3. 获取SqlSession，本质上其实就是connection对象的一个包装类
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession(true);

        // Connection connection = sqlSession1.getConnection();


        // 4. 执行对应的sql语句
        int affectedRows = sqlSession1.insert("cskaoyan.insertAccount");

        sqlSession1.commit();

        System.out.println("affectedRows:" + affectedRows);

    }
}
